#include "SingaporeBusParsers.h"

void ParseStationsL(const TDesC8& aBuf, RArray<TStation>& aStations, RArray<TPtrC8>& aServiceKeys)
	{
	const TUint8* p = aBuf.Ptr(), *endp = aBuf.Ptr() + aBuf.Length();
	const TUint8* b;

	TStation station;
	for( ; p != endp ; )
		{
		TReal64 latitude, longitude;
		TReal32 altitude;
		b = ++p; // ignore '[', b points on the start of the bus number
		while(*p != ']') ++p;
		station.iStationNumber.Set(b, p - b);
		b = ++(++p); // ignore "]\n", b points on the start of the type
		while(*p != '\n') ++p;
		TInt stationType;
		TLex8( TPtrC8(b, p - b) ).Val(stationType);
		station.iStationType = (TStation::TStationType)stationType;
		b = ++p; // ignore '\n', b points on the start of the address
		while(*p != '\n') ++p;
		station.iAddress.Set(b, p - b);		
		b = ++p; // ignore '\n', b points on the start of the nickname
		while(*p != '\n') ++p;
		station.iNickname.Set(b, p - b);		
		b = ++p; // ignore '\n', b points on the start of the 2nd address
		while(*p != '\n') ++p;
		station.iAddress2.Set(b, p - b);		
		b = ++p; // ignore '\n', b points on the start of the latitude
		while(*p != ',') ++p;
		TLex8( TPtrC8(b, p - b) ).Val(latitude);
		b = ++p; // ignore ',', b points on the start of the longitude
		while(*p != ',') ++p;
		TLex8( TPtrC8(b, p - b) ).Val(longitude);
		b = ++p; // ignore ',', b points on the start of the altitude
		while(*p != '\n') ++p;
		TLex8( TPtrC8(b, p - b) ).Val(altitude);
		station.iCoord.SetCoordinate(latitude, longitude, altitude);
		b = ++p; // ignore '\n', b points on the start of a service key
		station.iServicesBeg = aServiceKeys.Count();
		while(p != endp && *p != '[')
			{
			while(*p != '\n') ++p;
			TPtrC8 serviceKey(b, p - b);
			aServiceKeys.AppendL(serviceKey);
			b = ++p; // ignore '\n', b points on the start of a service key or on the end of the buffer or on the start of a new section started by '['			
			}
		station.iServicesEnd = aServiceKeys.Count();
		aStations.Append(station);
		}
	}


void ParseServicesL(const TDesC8& aBuf, RArray<TService>& aServices, RArray<TPtrC8>& aStationsKeys)
	{
	const TUint8* p = aBuf.Ptr(), *endp = aBuf.Ptr() + aBuf.Length();
	const TUint8* b;

	TService service;
	for( ; p != endp ; )
		{
		TInt direction;
		b = ++p; // ignore '[', b points on the start of the service key
		while(*p != ']') ++p;
		service.iServiceKey.Set(b, p - b);
		b = ++(++p); // ignore "]\n", b points on the start of the service name
		while(*p != '\n') ++p;
		service.iName.Set(b, p - b);		
		b = ++p; // ignore '\n', b points on the start of the operator
		while(*p != '\n') ++p;
		service.iOperator.Set(b, p - b);		
		b = ++p; // ignore '\n', b points on the start of the direction
		while(*p != '\n') ++p;
		TLex8( TPtrC8(b, p - b) ).Val(direction);
		service.iDirection = direction;
		b = ++p; // ignore '\n', b points on the start of the from
		while(*p != '\n') ++p;
		service.iFrom.Set(b, p - b);		
		b = ++p; // ignore '\n', b points on the start of the to
		while(*p != '\n') ++p;
		service.iTo.Set(b, p - b);		
		b = ++p; // ignore '\n'
		
		service.iStationsBeg = aStationsKeys.Count();
		while(p != endp && *p != '[')
			{
			while(*p != '\n') ++p;
			TPtrC8 station(b, p - b);
			aStationsKeys.AppendL(station);
			b = ++p; // ignore '\n', b points on the start of a station or on the end of the buffer or on the start of a new section started by '['			
			}
		service.iStationsEnd = aStationsKeys.Count();
		
		
		aServices.Append(service);
		}

	}

void ParseServicePathsL(const TDesC8& aBuf, RArray<TServicePath>& aBusPath, RArray<TCoordinate>& aCoordinates)
	{
	const TUint8* p = aBuf.Ptr(), *endp = aBuf.Ptr() + aBuf.Length();
	const TUint8* b;

	TServicePath servicePath;
	TCoordinate coord;
	for( ; p != endp ; )
		{
		TReal64 latitude, longitude;
		TReal32 altitude;
		b = ++p; // ignore '[', b points on the start of the service key
		while(*p != ']') ++p;
		servicePath.iServiceKey.Set(b, p - b);
		b = ++(++p); // ignore "]\n", b points on the start of the longitude
		servicePath.iPathBeg = aCoordinates.Count();
		while(p != endp && *p != '[')
			{
			while(*p != ',') ++p;
			TLex8( TPtrC8(b, p - b) ).Val(latitude);
			b = ++p; // ignore ',', b points on the start of the longitude
			while(*p != ',') ++p;
			TLex8( TPtrC8(b, p - b) ).Val(longitude);
			b = ++p; // ignore ',', b points on the start of the altitude
			while(*p != '\n') ++p;
			b = ++p; // ignore '\n'
			TLex8( TPtrC8(b, p - b) ).Val(altitude);
			coord.SetCoordinate(latitude, longitude, altitude);
			aCoordinates.Append(coord);
			}
		servicePath.iPathEnd = aCoordinates.Count();
		aBusPath.AppendL(servicePath);
		}
	
	}

void ParseIndexL(const TDesC8& aBuf, RArray<TIndexItem>& aIndex)
	{
	const TUint8* p = aBuf.Ptr(), *endp = aBuf.Ptr() + aBuf.Length();
	const TUint8* b;

	TIndexItem item;
	for( ; p != endp ; )
		{
		TReal64 offset, size;
		b = ++p; // ignore '[', b points on the start of the service key
		while(*p != ']') ++p;
		item.iKey.Set(b, p - b);
		b = ++(++p); // ignore "]\n", b points on the start of the service name
		while(*p != '\n') ++p;
		TLex8( TPtrC8(b, p - b) ).Val(offset);
		b = ++p; // ignore '\n', b points on the start of the longitude
		while(*p != '\n') ++p;
		TLex8( TPtrC8(b, p - b) ).Val(size);
		b = ++p; // ignore '\n'
		item.iOffset = offset;
		item.iSize = size;
		aIndex.Append(item);
		}	
	}
